import { getProxyPath } from '@/config/proxyPath'
import { createRequest } from '@/utils/mc-request/index'
import { addparam as ad } from './addparam'

/**crud api */
export class CrudApi {
  /**
   * @param {string} proxyKey
   * @param {string} beforeUrl
   */
  constructor(proxyKey, beforeUrl, pathMap) {
    this.proxyKey = proxyKey
    this.beforeUrl = beforeUrl
    this.request = createRequest({ baseURL: getProxyPath(proxyKey) })
    this.pathMap = Object.assign(
      {
        addition: 'save',
        update: 'update',
        getById: 'getById',
        getPager: 'page',
        getList: 'list',
        delete: 'delete'
      },
      pathMap
    )
  }
  /**新增 */
  addition(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.addition}`, ad(data), { debounce: true })
  }
  /**编辑 */
  update(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.update}`, ad(data), { debounce: true })
  }
  /**详情 */
  getById(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.getById}`, ad(data))
  }
  /**分页 */
  getPager(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.getPager}`, ad(data))
  }
  /**列表 */
  getList(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.getList}`, ad(data))
  }
  /**删除 */
  delete(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.delete}`, ad(data))
  }
}

/**crud api */
export class FcrudApi {
  /**
   * @param {string} proxyKey
   * @param {string} beforeUrl
   */
  constructor(proxyKey, beforeUrl, pathMap) {
    this.proxyKey = proxyKey
    this.beforeUrl = beforeUrl
    this.request = createRequest({ baseURL: getProxyPath(proxyKey) })
    this.pathMap = Object.assign(
      { save: 'save', update: 'update', get: 'get', getPage: 'getPage', getList: 'getList', delete: 'delete' },
      pathMap
    )
  }
  /**保存 */
  save(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.save}`, ad(data), { debounce: true })
  }
  /**更新 */
  update(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.update}`, ad(data), { debounce: true })
  }
  /**详情 */
  get(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.get}`, ad(data))
  }
  /**分页 */
  getPage(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.getPage}`, ad(data))
  }
  /**列表 */
  getList(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.getList}`, ad(data))
  }
  /**删除 */
  delete(data) {
    return this.request.postJson(`/${this.beforeUrl}/${this.pathMap.delete}`, ad(data))
  }
}
